package com.han.hotelplat.service.datacenter.finance.impl;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Hibernate;
import org.hibernate.type.NullableType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.han.hotelplat.comm.Constant;
import com.han.hotelplat.comm.base.EntityManage;
import com.han.hotelplat.comm.utils.AreaHelp;
import com.han.hotelplat.comm.utils.DateUtil;
import com.han.hotelplat.comm.utils.StrUtil;
import com.han.hotelplat.service.datacenter.finance.FinanceService;

@Service
public class FinanceServiceImpl implements FinanceService{
	@Autowired
	private  EntityManage entityManage;
	private DecimalFormat   moneyfnum  =   new  DecimalFormat("##0.00");  
	private DecimalFormat   roomfnum  =   new  DecimalFormat("##0.0");  
	public Map<String, Object> getFinanceTotalGroupResult(String startTime, String endTime, Integer type){
		Map<String, Object> data = new HashMap<String, Object>();
		float hotelAdvanceCost = 0f;//酒店预支
		double hotelRealCost = 0d;//酒店实际支出
		float emAdvanceReward = 0f;//帮工预收入
		float emRealReward = 0f;//帮工实际收入
		float platformAdvanceReward = 0f;//平台预收入
		double platformRealReward = 0f;//平台实际收入
		StringBuffer sql = new StringBuffer("SELECT ct.result, SUM(ct.costmoney),SUM(ct.reward) FROM tb_child_task AS ct WHERE ct.result>1 AND ct.result<>5");
		//StringBuffer sql2 = new StringBuffer("SELECT SUM(amount) FROM tb_bill AS b, tb_wallet AS w, tb_hotel AS h  WHERE w.id=b.expwalletid AND w.usertype=1 AND w.userid=h.id ");
		
		
		if(type != null && type.intValue() >= 0){
			switch (type) {
			case 0:
				sql.append(" AND TO_DAYS(date_format(ct.creattime,'%Y-%m-%d')) = TO_DAYS(now()) ");
				//sql2.append(" AND TO_DAYS(date_format(b.createtime,'%Y-%m-%d')) = TO_DAYS(now()) ");
				break;
			case 1:
				sql.append(" AND YEARWEEK(date_format(ct.creattime,'%Y-%m-%d'),1) = YEARWEEK(now(),1) ");
				//sql2.append(" AND YEARWEEK(date_format(b.createtime,'%Y-%m-%d'),1) = YEARWEEK(now(),1) ");
				break;
			case 2:
				sql.append(" AND DATE_FORMAT( ct.creattime, '%Y%m' ) = DATE_FORMAT( CURDATE() , '%Y%m' ) ");
				//sql2.append(" AND DATE_FORMAT( b.createtime, '%Y%m' ) = DATE_FORMAT( CURDATE() , '%Y%m' ) ");
				break;
			default:
				break;
			}
		}else{
			if(StringUtils.isNotBlank(startTime)){
				sql.append(" AND ct.creattime >= '"+startTime+"'");
				//sql2.append(" AND b.createtime >= '"+startTime+"'");
			}else{
				data.put("result", -1);
				data.put("msg", "请输入起始日期");
				return data;
			}
			if(StringUtils.isNotBlank(endTime)){
				sql.append(" AND ct.creattime <= '"+endTime+" 23:59:59'");
				//sql2.append(" AND b.createtime <= '"+endTime+" 23:59:59'");
			}
		}
		sql.append("GROUP BY ct.result");
		Map<String, NullableType> fieldMappingType = new HashMap<String, NullableType>();		
		//List<Object> ctli = entityManage.findBySql(sql2.toString());
		fieldMappingType.put("money", Hibernate.DOUBLE);
		/*for(int i= 0 ; i<ctli.size(); i++){
			hotelRealCost= (double) (ctli.get(i) == null ? 0d:ctli.get(i));
		}*/
		
		List<Object> ctli = entityManage.findBySql(sql.toString());
		for(int i= 0 ; i<ctli.size(); i++){
			Object[] ory = (Object[]) ctli.get(i);
			int result = 0;
			if(ory[0] != null){//审核结果
				result =Integer.parseInt(ory[0].toString());
			}
			hotelAdvanceCost = hotelAdvanceCost + Float.parseFloat(ory[1].toString());
			emAdvanceReward = emAdvanceReward + Float.parseFloat(ory[2].toString());
			if(result == Constant.ATTENDANCE_END){//确认完成
				emRealReward = emRealReward + Float.parseFloat(ory[2].toString());
				hotelRealCost= hotelRealCost + Float.parseFloat(ory[1].toString());
			}
		}
		platformAdvanceReward = hotelAdvanceCost - emAdvanceReward;
		platformRealReward = hotelRealCost - emRealReward;
		data.put("platformAdvanceReward", moneyfnum.format(platformAdvanceReward));
		data.put("platformRealReward", moneyfnum.format(platformRealReward));
		data.put("hotelAdvanceCost", moneyfnum.format(hotelAdvanceCost));
		data.put("hotelRealCost", moneyfnum.format(hotelRealCost));
		data.put("emAdvanceReward", moneyfnum.format(emAdvanceReward));
		data.put("emRealReward", moneyfnum.format(emRealReward));
		data.put("result", 0);
		data.put("msg", "数据获取成功");
		return data;
	}
	@Override
	public Map<String, Object> getFinanceHotelCostGroupAreaResult(String startTime,
			String endTime, Integer type, Integer groupArea) {
		String aCode = "substr(h.area,1,4 )";
		if(groupArea != null && groupArea.intValue() == 1){//按市级归类
			aCode = "substr(h.area,1,2)";
		}else if(groupArea == null || groupArea.intValue() == 2){//按市级归类
			aCode = "substr(h.area,1,4)";
		}else if(groupArea != null && groupArea.intValue() == 3){//按区级归类
			aCode = "substr(h.area,1,6)";
		}
		Map<String, Object> data = new HashMap<String, Object>();
		//查询已注册的所有酒店的区域编码
		String sql1 = "SELECT "+aCode+" FROM tb_hotel AS h group by "+aCode;
		List<Object> cityLi = entityManage.findBySql(sql1.toString());
		Set<String> cityCode = new HashSet<String>();
		for(int i= 0 ; i<cityLi.size(); i++){
			if(cityLi.get(i) == null){
				continue;
			}
			cityCode.add(cityLi.get(i).toString());
		}
		//预计支出
		Map<String, Float> hotelAdvanceCostData = new HashMap<String, Float>();
		//实际支出
		Map<String, Float> hotelRealCostData = new HashMap<String, Float>();
		
		StringBuffer sql = new StringBuffer("SELECT "+aCode+", ct.result, SUM(ct.costmoney) FROM tb_child_task AS ct, tb_task AS t, tb_hotel AS h WHERE ct.taskid=t.taskid AND h.id=t.hotelid AND ct.result IN(2,3,4) ");
		StringBuffer sql2 = new StringBuffer("SELECT "+aCode+", SUM(amount) FROM tb_bill AS b, tb_wallet AS w, tb_hotel AS h  WHERE w.id=b.expwalletid AND w.usertype=1 AND w.userid=h.id ");
		
		if(type != null && type.intValue() >= 0){
			switch (type) {
			case 0:
				sql.append(" AND TO_DAYS(date_format(ct.creattime,'%Y-%m-%d')) = TO_DAYS(now()) ");
				sql2.append(" AND TO_DAYS(date_format(b.createtime,'%Y-%m-%d')) = TO_DAYS(now()) ");
				break;
			case 1:
				sql.append(" AND YEARWEEK(date_format(ct.creattime,'%Y-%m-%d'),1) = YEARWEEK(now(),1) ");
				sql2.append(" AND YEARWEEK(date_format(b.createtime,'%Y-%m-%d'),1) = YEARWEEK(now(),1) ");
				break;
			case 2:
				sql.append(" AND DATE_FORMAT( ct.creattime, '%Y%m' ) = DATE_FORMAT( CURDATE() , '%Y%m' ) ");
				sql2.append(" AND DATE_FORMAT( b.createtime, '%Y%m' ) = DATE_FORMAT( CURDATE() , '%Y%m' ) ");
				break;
			default:
				break;
			}
		}else{
			if(StringUtils.isNotBlank(startTime)){
				sql.append(" AND ct.creattime >= '"+startTime+"'");
				sql2.append(" AND b.createtime >= '"+startTime+"'");
			}else{
				data.put("result", -1);
				data.put("msg", "请输入起始日期");
				return data;
			}
			
			if(StringUtils.isNotBlank(endTime)){
				sql.append(" AND ct.creattime <= '"+endTime+" 23:59:59'");
				sql2.append(" AND b.createtime <= '"+endTime+" 23:59:59'");
			}
		}
		sql.append("group by "+aCode+",ct.result");
		sql2.append("group by "+aCode);
		List<Object> ctli = entityManage.findBySql(sql2.toString());
		Set<String> tmp = new HashSet<String>();
		for(int i= 0 ; i<ctli.size(); i++){
			String areaCode = "0";//日期
			float money = 0;//金额
			Object[] ory = (Object[]) ctli.get(i);
			if(ory[0] != null){
				areaCode = ory[0].toString();
			}
			if(ory[1] != null){
				money = Float.parseFloat(ory[1].toString());
			}
			tmp.add(areaCode);
			hotelRealCostData.put(areaCode, money);
		}
		
		ctli.clear();
		ctli = entityManage.findBySql(sql.toString());
		
		for(int i= 0 ; i<ctli.size(); i++){
			String areaCode = "0";//地区
			int result = 0; //结果
			float count = 0;//金额
			
			Object[] ory = (Object[]) ctli.get(i);
			if(ory[0] != null){//房量
				areaCode = ory[0].toString();
				tmp.add(areaCode);
			}
			if(ory[1] != null){//地区编码
				result = Integer.parseInt(ory[1].toString());
			}
			count = Float.parseFloat(ory[2].toString());
			Float old = hotelAdvanceCostData.get(areaCode);
			if(old == null){//预计支出
				hotelAdvanceCostData.put(areaCode, count);
			}else{
				hotelAdvanceCostData.put(areaCode, old+count);
			}
		}
		for(String areaCode : cityCode){
			if(!tmp.contains(areaCode)){//未提交过房量的地区
				hotelRealCostData.put(areaCode, 0f);
				hotelAdvanceCostData.put(areaCode, 0f);
			}
		}
		JSONArray array = new JSONArray();
		hotelRealCostData = StrUtil.sortMapByValue(hotelRealCostData);
		for(Entry<String, Float> en : hotelRealCostData.entrySet()){
			JSONObject js = new JSONObject();
			Integer area = 0;
			String code = en.getKey();
			String areaName = "";
			if(groupArea != null && groupArea.intValue() == 1){//按省级归类
				area = Integer.parseInt(code)*10000;
				areaName= AreaHelp.getInstance().getProvince(area);
			}else if(groupArea == null || groupArea.intValue() == 2){//按市级归类
				area = Integer.parseInt(code)*100;
				areaName = AreaHelp.getInstance().getCity(area);
			}else if(groupArea != null && groupArea.intValue() == 3){//按区级归类
				area = Integer.parseInt(code);
				areaName = AreaHelp.getInstance().getAreaModel(code).getName();
			}
			
			js.put(areaName,moneyfnum.format(en.getValue()));
			array.add(js);
		}
		data.put("hotelRealCostData", array);
		
		JSONArray array2 = new JSONArray();
		hotelAdvanceCostData = StrUtil.sortMapByValue(hotelAdvanceCostData);
		for(Entry<String, Float> en : hotelAdvanceCostData.entrySet()){
			JSONObject js = new JSONObject();
			Integer area = 0;
			String code = en.getKey();
			String areaName = "";
			if(groupArea != null && groupArea.intValue() == 1){//按省级归类
				area = Integer.parseInt(code)*10000;
				areaName= AreaHelp.getInstance().getProvince(area);
			}else if(groupArea == null || groupArea.intValue() == 2){//按市级归类
				area = Integer.parseInt(code)*100;
				areaName = AreaHelp.getInstance().getCity(area);
			}else if(groupArea != null && groupArea.intValue() == 3){//按区级归类
				area = Integer.parseInt(code);
				areaName = AreaHelp.getInstance().getAreaModel(code).getName();
			}
			
			js.put(areaName,moneyfnum.format(en.getValue()));
			array2.add(js);
		}
		data.put("hotelAdvanceCostData", array2);
		data.put("result", 0);
		data.put("msg", "数据获取成功");
		return data;
	}
	
	@Override
	public Map<String, Object> getFinanceEmRewardGroupAreaResult(String startTime,
			String endTime, Integer type, Integer groupArea) {
		String aCode = "substr(h.area,1,4 )";
		if(groupArea != null && groupArea.intValue() == 1){//按市级归类
			aCode = "substr(h.area,1,2)";
		}else if(groupArea == null || groupArea.intValue() == 2){//按市级归类
			aCode = "substr(h.area,1,4)";
		}else if(groupArea != null && groupArea.intValue() == 3){//按区级归类
			aCode = "substr(h.area,1,6)";
		}
		Map<String, Object> data = new HashMap<String, Object>();
		//查询已注册的所有酒店的区域编码
		String sql1 = "SELECT "+aCode+" FROM tb_hotel AS h group by "+aCode;
		List<Object> cityLi = entityManage.findBySql(sql1.toString());
		Set<String> cityCode = new HashSet<String>();
		for(int i= 0 ; i<cityLi.size(); i++){
			if(cityLi.get(i) == null){
				continue;
			}
			cityCode.add(cityLi.get(i).toString());
		}
		//预计支出
		Map<String, Float> emAdvanceRewardData = new HashMap<String, Float>();
		//实际支出
		Map<String, Float> emRealRewardData = new HashMap<String, Float>();
		
		StringBuffer sql = new StringBuffer("SELECT "+aCode+",ct.result, SUM(ct.reward) AS reward FROM tb_child_task AS ct, tb_task AS t, tb_hotel AS h WHERE ct.taskid=t.taskid AND h.id=t.hotelid AND ct.result IN(2,3,4) ");
		if(type != null && type.intValue() >= 0){
			switch (type) {
			case 0:
				sql.append(" AND TO_DAYS(date_format(ct.creattime,'%Y-%m-%d')) = TO_DAYS(now()) ");
				break;
			case 1:
				sql.append(" AND YEARWEEK(date_format(ct.creattime,'%Y-%m-%d'),1) = YEARWEEK(now(),1) ");
				break;
			case 2:
				sql.append(" AND DATE_FORMAT( ct.creattime, '%Y%m' ) = DATE_FORMAT( CURDATE() , '%Y%m' ) ");
				break;
			default:
				break;
			}
		}else{
			if(StringUtils.isNotBlank(startTime)){
				sql.append(" AND ct.creattime >= '"+startTime+"'");
			}else{
				data.put("result", -1);
				data.put("msg", "请输入起始日期");
				return data;
			}
			
			if(StringUtils.isNotBlank(endTime)){
				sql.append(" AND ct.creattime <= '"+endTime+" 23:59:59'");
			}
		}
		sql.append("group by "+aCode+",ct.result ORDER BY reward DESC");
		List<Object> ctli = entityManage.findBySql(sql.toString());
		Set<String> tmp = new HashSet<String>();
		for(int i= 0 ; i<ctli.size(); i++){
			String areaCode = "0";//地区
			int result = 0; //结果
			float count = 0;//金额
			
			Object[] ory = (Object[]) ctli.get(i);
			if(ory[0] != null){//房量
				areaCode = ory[0].toString();
				tmp.add(areaCode);
			}
			if(ory[1] != null){//地区编码
				result = Integer.parseInt(ory[1].toString());
			}
			count = Float.parseFloat(ory[2].toString());
			Float old = emAdvanceRewardData.get(areaCode);
			
			if(old == null){//预计支出
				emAdvanceRewardData.put(areaCode, count);
			}else{
				emAdvanceRewardData.put(areaCode, old+count);
			}
			
			if(result == 4){//实际支出
				old = emRealRewardData.get(areaCode);
				if(old == null){
					emRealRewardData.put(areaCode, count);
				}else{
					emRealRewardData.put(areaCode, old+count);
				}
			}
		}
		for(String areaCode : cityCode){
			if(!tmp.contains(areaCode)){//未提交过房量的地区
				emRealRewardData.put(areaCode, 0f);
				emAdvanceRewardData.put(areaCode, 0f);
			}
		}
		JSONArray array = new JSONArray();
		emRealRewardData = StrUtil.sortMapByValue(emRealRewardData);
		for(Entry<String, Float> en : emRealRewardData.entrySet()){
			JSONObject js = new JSONObject();
			Integer area = 0;
			String code = en.getKey();
			String areaName = "";
			if(groupArea != null && groupArea.intValue() == 1){//按省级归类
				area = Integer.parseInt(code)*10000;
				areaName= AreaHelp.getInstance().getProvince(area);
			}else if(groupArea == null || groupArea.intValue() == 2){//按市级归类
				area = Integer.parseInt(code)*100;
				areaName = AreaHelp.getInstance().getCity(area);
			}else if(groupArea != null && groupArea.intValue() == 3){//按区级归类
				area = Integer.parseInt(code);
				areaName = AreaHelp.getInstance().getAreaModel(code).getName();
			}
			
			
			js.put(areaName, moneyfnum.format(en.getValue()));
			array.add(js);
		}
		data.put("emRealRewardData", array);
		
		JSONArray array2 = new JSONArray();
		emAdvanceRewardData = StrUtil.sortMapByValue(emAdvanceRewardData);
		for(Entry<String, Float> en : emAdvanceRewardData.entrySet()){
			JSONObject js = new JSONObject();
			Integer area = 0;
			String code = en.getKey();
			String areaName = "";
			if(groupArea != null && groupArea.intValue() == 1){//按省级归类
				area = Integer.parseInt(code)*10000;
				areaName= AreaHelp.getInstance().getProvince(area);
			}else if(groupArea == null || groupArea.intValue() == 2){//按市级归类
				area = Integer.parseInt(code)*100;
				areaName = AreaHelp.getInstance().getCity(area);
			}else if(groupArea != null && groupArea.intValue() == 3){//按区级归类
				area = Integer.parseInt(code);
				areaName = AreaHelp.getInstance().getAreaModel(code).getName();
			}
			
			
			js.put(areaName, moneyfnum.format(en.getValue()));
			array2.add(js);
		}
		data.put("result", 0);
		data.put("msg", "数据获取成功");
		data.put("emAdvanceRewardData", array2);
		return data;
	}
	@Override
	public Map<String, Object> getFinanceHotelCostGroupDateResult(
			String startTime, String endTime, Integer type, Integer areaCode) {
		Map<String, Object> data = new HashMap<String, Object>();
		//预计支出
		Map<String, Float> hotelAdvanceCostData = new HashMap<String, Float>();
		//实际支出
		Map<String, Float> hotelRealCostData = new HashMap<String, Float>();
		//提交房量
		Map<String, Float> hotelAdvanceRoomData = new HashMap<String, Float>();
		//确认房量
		Map<String, Float> hotelRealRoomData = new HashMap<String, Float>();
				
		Date startDate = new Date();
		Date endDate = new Date();
		StringBuffer sql = new StringBuffer("SELECT substr(ct.creattime,1,10),ct.result, SUM(ct.costmoney), SUM(ct.roomcount) FROM tb_child_task AS ct, tb_task AS t, tb_hotel AS h WHERE ct.taskid=t.taskid AND h.id=t.hotelid AND ct.result IN(2,3,4) ");
		StringBuffer sql2 = new StringBuffer("SELECT substr(b.createtime,1,10), SUM(amount) FROM tb_bill AS b, tb_wallet AS w, tb_hotel AS h  WHERE w.id=b.expwalletid AND w.usertype=1 AND w.userid=h.id ");
		
		if(type != null && type>=0){
			switch (type) {
			case 0://过去7天
				startDate = DateUtil.addDay(new Date(), -6);
				startTime = DateUtil.formatYMD(startDate);
				break;
			case 1://过去30天
				startDate = DateUtil.addDay(new Date(), -29);
				startTime = DateUtil.formatYMD(startDate);
				break;
			default:
				startDate = DateUtil.addDay(new Date(), -6);
				startTime = DateUtil.formatYMD(startDate);
				break;
			}
			sql.append(" AND ct.creattime >= '"+startTime+"'");
			sql2.append(" AND b.createtime >= '"+startTime+"'");
		}else{
			if(StringUtils.isNotBlank(startTime)){
				startDate = DateUtil.parseYMD(startTime);
				sql.append(" AND ct.creattime >= '"+startTime+"'");
				sql2.append(" AND b.createtime >= '"+startTime+"'");
			}else{
				data.put("result", -1);
				data.put("msg", "请输入起始时间");
				return data;
			}
			
			if(StringUtils.isNotBlank(endTime)){
				endDate = DateUtil.parseYMD(endTime);
				sql.append(" AND ct.creattime <= '"+endTime+" 23:59:59'");
				sql2.append(" AND b.createtime <= '"+endTime+" 23:59:59'");
			}
		}
		
		if(areaCode != null && areaCode.intValue()>100000){//区域码至少6位
			String w_areaCode = areaCode.toString();
			int x=0;
			String w_area="";
			if(w_areaCode.endsWith("0000")) {
				//精度 省份
				x=9999;
			}else if(w_areaCode.endsWith("00")) {
				//精度 市
				x=99;
			}else {
				//精度 区
				x=0;
			}
			//要的   - 条件  >=0
			//410610-410000 <= 9999
			//410610-410600 <= 99
			//410610-410610 == 0
			w_area=" and convert(h.area,signed) - "+areaCode +" >=0 ";
			w_area+=" and convert(h.area,signed) - "+areaCode +" <= "+x;
			sql.append(w_area);
			sql2.append(w_area);
		}
		
		sql.append(" group by substr(ct.creattime,1,10),ct.result");
		sql2.append(" group by substr(b.createtime,1,10)");
		List<Object> ctli = entityManage.findBySql(sql2.toString());
		Map<String, Float> costMoneyMap = new HashMap<String, Float>();
		for(int i= 0 ; i<ctli.size(); i++){
			String date = "0";//日期
			float money = 0;//金额
			
			Object[] ory = (Object[]) ctli.get(i);
			if(ory[0] != null){
				date = ory[0].toString();
			}
			if(ory[1] != null){
				money = Float.parseFloat(ory[1].toString());
			}
			costMoneyMap.put(date, money);
		}
		
		ctli.clear();
		ctli = entityManage.findBySql(sql.toString());
		for(int i= 0 ; i<ctli.size(); i++){
			String date = "0";//日期
			int result = 0; //结果
			float money = 0;//金额
			float count = 0;//房量
			
			Object[] ory = (Object[]) ctli.get(i);
			if(ory[0] != null){
				date = ory[0].toString();
			}
			if(ory[1] != null){
				result = Integer.parseInt(ory[1].toString());
			}
			//金额
			money = Float.parseFloat(ory[2].toString());
			//房量
			count = Float.parseFloat(ory[3].toString());
			Float oldMoney = hotelAdvanceCostData.get(date);
			if(oldMoney == null){//预计支出
				hotelAdvanceCostData.put(date, money);
			}else{
				hotelAdvanceCostData.put(date, oldMoney+money);
			}
			//房量
			Float oldCount = hotelAdvanceRoomData.get(date);
			if(oldCount == null){//提交房量
				hotelAdvanceRoomData.put(date, count);
			}else{
				hotelAdvanceRoomData.put(date, oldCount+count);
			}
			
			if(oldMoney == null){//预计花费
				hotelAdvanceCostData.put(date, money);
			}else{
				hotelAdvanceCostData.put(date, oldMoney+money);
			}
			if(result == Constant.ATTENDANCE_HASAUTH || result == Constant.ATTENDANCE_END){//实际支出
				oldMoney = hotelRealCostData.get(date);
				oldCount = hotelRealRoomData.get(date);
				if(oldMoney == null){
					hotelRealCostData.put(date, money);
				}else{
					hotelRealCostData.put(date, oldMoney+money);
				}
				
				if(oldCount == null){//提交房量
					hotelRealRoomData.put(date, count);
				}else{
					hotelRealRoomData.put(date, oldCount+count);
				}
			}
		}
		
		JSONArray array = new JSONArray();
		List<Date> allDate = DateUtil.getBetweenDates(startDate, endDate);
		for(Date d : allDate){
			JSONObject js = new JSONObject();
			String date =  DateUtil.formatYMD(d);
			js.put("date", date);
			js.put("advanceCostMoney", moneyfnum.format(hotelAdvanceCostData.get(date) == null ? 0 : hotelAdvanceCostData.get(date)));
			js.put("realCostMoney", moneyfnum.format(costMoneyMap.get(date) == null ? 0 : costMoneyMap.get(date)));
			js.put("advanceRoomCount", roomfnum.format(hotelAdvanceRoomData.get(date) == null ? 0 : hotelAdvanceRoomData.get(date)));
			js.put("realRoomCount", roomfnum.format(hotelRealRoomData.get(date)== null?0:hotelRealRoomData.get(date)));
			array.add(js);
		}
		data.put("hotelCostData", array);
		data.put("result", 0);
		data.put("msg", "数据获取成功");
		return data;
	}
	
	@Override
	public Map<String, Object> getFinanceEmRewardGroupDateResult(
			String startTime, String endTime, Integer type, Integer area) {
		Map<String, Object> data = new HashMap<String, Object>();
		//预计支出
		Map<String, Float> emAdvanceRewardData = new HashMap<String, Float>();
		//实际支出
		Map<String, Float> emRealRewardData = new HashMap<String, Float>();
		
		//提交房量
		Map<String, Float> hotelAdvanceRoomData = new HashMap<String, Float>();
		//确认房量
		Map<String, Float> hotelRealRoomData = new HashMap<String, Float>();
		
		Date startDate = new Date();
		Date endDate = new Date();
		
		StringBuffer sql = new StringBuffer("SELECT substr(ct.creattime,1,10),ct.result, SUM(ct.reward), SUM(ct.roomcount) FROM tb_child_task AS ct, tb_task AS t, tb_hotel AS h WHERE ct.taskid=t.taskid AND h.id=t.hotelid AND ct.result IN(2,3,4) ");
		if(type != null && type>=0){
			switch (type) {
			case 0://过去7天
				startDate = DateUtil.addDay(new Date(), -6);
				startTime = DateUtil.formatYMD(startDate);
				break;
			case 1://过去30天
				startDate = DateUtil.addDay(new Date(), -29);
				startTime = DateUtil.formatYMD(startDate);
				break;
			default:
				startDate = DateUtil.addDay(new Date(), -6);
				startTime = DateUtil.formatYMD(startDate);
				break;
			}
			sql.append(" AND ct.creattime >= '"+startTime+"'");
		}else{
			if(StringUtils.isNotBlank(startTime)){
				sql.append(" AND ct.creattime >= '"+startTime+"'");
				startDate = DateUtil.parseYMD(startTime);
			}else{
				data.put("result", -1);
				data.put("msg", "请输入起始时间");
				return data;
			}
			
			if(StringUtils.isNotBlank(endTime)){
				sql.append(" AND ct.creattime <= '"+endTime+" 23:59:59'");
				endDate = DateUtil.parseYMD(endTime);
			}
		}
		
		if(area != null && area.intValue()>100000){//区域码至少6位
			String w_areaCode = area.toString();
			int x=0;
			String w_area="";
			if(w_areaCode.endsWith("0000")) {
				//精度 省份
				x=9999;
			}else if(w_areaCode.endsWith("00")) {
				//精度 市
				x=99;
			}else {
				//精度 区
				x=0;
			}
			//要的   - 条件  >=0
			//410610-410000 <= 9999
			//410610-410600 <= 99
			//410610-410610 == 0
			w_area=" and convert(h.area,signed) - "+area +" >=0 ";
			w_area+=" and convert(h.area,signed) - "+area +" <= "+x;
			sql.append(w_area);
		}
		
		sql.append(" group by substr(ct.creattime,1,10),ct.result");
		List<Object> ctli = entityManage.findBySql(sql.toString());
		for(int i= 0 ; i<ctli.size(); i++){
			String date = "0";//日期
			int result = 0; //结果
			float money = 0;//金额
			float count = 0;//房量
			
			Object[] ory = (Object[]) ctli.get(i);
			if(ory[0] != null){
				date = ory[0].toString();
			}
			if(ory[1] != null){
				result = Integer.parseInt(ory[1].toString());
			}
			money = Float.parseFloat(ory[2].toString());
			count = Float.parseFloat(ory[3].toString());
			Float oldMoney = emAdvanceRewardData.get(date);
			Float oldCount = hotelAdvanceRoomData.get(date);
			
			if(oldMoney == null){//预计收入
				emAdvanceRewardData.put(date, money);
			}else{
				emAdvanceRewardData.put(date, oldMoney + money);
			}
			if(oldCount == null){
				hotelAdvanceRoomData.put(date, count);
			}else{
				hotelAdvanceRoomData.put(date, oldCount + count);
			}
			
			if(result == Constant.ATTENDANCE_END){//实际收入
				oldMoney = emRealRewardData.get(date);
				oldCount = hotelRealRoomData.get(date);
				if(oldMoney == null){
					emRealRewardData.put(date, money);
					hotelRealRoomData.put(date, count);
				}else{
					emRealRewardData.put(date, oldMoney + money);
					hotelRealRoomData.put(date, oldCount + count);
				}
			}
		}
		
		JSONArray array = new JSONArray();
		List<Date> allDate = DateUtil.getBetweenDates(startDate, endDate);
		for(Date d : allDate){
			JSONObject js = new JSONObject();
			String date = DateUtil.formatYMD(d);
			js.put("date", date);
			js.put("advanceRewardMoney", moneyfnum.format(emAdvanceRewardData.get(date) == null ? 0 : emAdvanceRewardData.get(date)));
			js.put("realRewardMoney", moneyfnum.format(emRealRewardData.get(date) == null ? 0 : emRealRewardData.get(date)));
			js.put("advanceRoomCount", roomfnum.format(hotelAdvanceRoomData.get(date) == null ? 0 : hotelAdvanceRoomData.get(date)));
			js.put("realRoomCount", roomfnum.format(hotelRealRoomData.get(date) == null ? 0 : hotelRealRoomData.get(date)));
			array.add(js);
		}
		data.put("emRewardData", array);
		data.put("result", 0);
		data.put("msg", "数据获取成功");
		return data;
	}
}
